/**
 * Notes: 系统服务
 * Date: 2023-09-05 19:20:00 
 */

const BaseProjectService = require('./base_project_service.js');
const util = require('../../../framework/helpers/util.js');
const cloudBase = require('../../../framework/cloud/cloud_base.js');

class SystemService extends BaseProjectService {

	/** 构造函数 */
	constructor() {
		super();
	}
	
	/**
	 * 检查数据库集合状态
	 * @param {Array} collections 需要检查的集合列表
	 * @returns {Promise<Object>} 检查结果
	 */
	async checkDbCollections(collections = []) {
		if (!collections || !Array.isArray(collections) || collections.length === 0) {
			return { code: 1, message: '请提供要检查的集合列表' };
		}
		
		try {
			// 获取数据库实例
			const db = cloudBase.getDb();
			
			// 所有集合列表
			const { data: allCollections } = await db.collections();
			const collectionNames = allCollections.map(collection => collection.name);
			
			console.log('数据库所有集合:', collectionNames);
			
			// 检查必要集合是否存在
			let results = {};
			results.collections = [];
			
			for (const collection of collections) {
				if (collectionNames.includes(collection)) {
					results.collections.push(collection);
				}
			}
			
			// 检查结果
			results.allExist = collections.every(c => results.collections.includes(c));
			results.missing = collections.filter(c => !results.collections.includes(c));
			
			return results;
		} catch (error) {
			console.error('检查数据库集合失败:', error);
			return { 
				code: 500, 
				message: '检查数据库集合失败:' + error.message,
				error: error
			};
		}
	}
}

module.exports = SystemService; 